require "Common/define"
require "Logic/Config"
require "Common/functions"


local json = require "cjson"

SendMsg = {};
local this = SendMsg;
--local msg_type = Config.msg_type;

local testTime = 0;




--参数1:消息类型，2:字段值
function SendMsg.Send( ... )
	
	local fType = select('1', ...);
	local fields = msg_fields[fType];

	local arg = { ... };
	if #fields+1 ~= #arg then
		logError("参数长度与协议不匹配："..fType)
		return;
	end

	local nTable = {}
	for i = 1, #fields do
		local sTable = {};
		local k = fields[i];
		local v = select(i+1, ...);
		sTable[k] = v;
		table.insert(nTable,sTable);
	end

	if #nTable == 0 then
		local set = {};
		set["none"] = 0;
		table.insert(nTable,set)
	end
	local msgJson = json.encode(nTable);
	WebMsgMgr:SendMsg(fType,tostring(msgJson));

	
	UIWaiting.ShowWaiting(fType);
	
	--[[local test = Time.realtimeSinceStartup - testTime;
	if test < 0.2 then
		logError("发送消息太快："..fType);
	end
	testTime = Time.realtimeSinceStartup;--]]
end

--特殊消息发送，参数为table
function SendMsg.SendTable(type,table)
	local msgJson = json.encode(table);
	WebMsgMgr:SendMsg(type,tostring(msgJson));
	UIWaiting.ShowWaiting(type);
end

--特殊消息发送的组合体
function SendMsg.SendTableCombine(myType,...)
    local tSend = {};
	local arg = { ... };
	for j=1,#arg do
		local v = select(j, ...);
        local tab = {};
        if j%2 == 1 then
            local msgValue = select(j+1, ...);
            tab[v] = msgValue;
            table.insert(tSend, tab);
        end
	end
    SendMsg.SendTable(myType,tSend);
	UIWaiting.ShowWaiting(myType);
end
